# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Ray Donnelly <mingw.android@gmail.com>

_realname=docutils
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python2-${_realname}" "${MINGW_PACKAGE_PREFIX}-python3-${_realname}")
pkgver=0.12
pkgrel=5
pkgdesc="Set of tools for processing plaintext docs into formats such as HTML, XML, or LaTeX (mingw-w64)"
arch=('any')
license=('custom')
url="http://docutils.sourceforge.net"
makedepends=("${MINGW_PACKAGE_PREFIX}-python2" "${MINGW_PACKAGE_PREFIX}-python3")
source=("https://downloads.sourceforge.net/${_realname}/${_realname}-${pkgver}.tar.gz")
sha256sums=('c7db717810ab6965f66c8cf0398a98c9d8df982da39b4cd7f162911eb89596fa')

build() {
  cd "${srcdir}/${_realname}-${pkgver}"
  ${MINGW_PREFIX}/bin/python3 setup.py build --build-lib=build/python3
  find build/python3 -type f -exec \
    sed -i '1s,^#! \?/usr/bin/\(env \|\)python$,#!/usr/bin/python3,' {} \;

  ${MINGW_PREFIX}/bin/python2 setup.py build --build-lib=build/python2
  find build/python2 -type f -exec \
    sed -i '1s,^#! \?/usr/bin/\(env \|\)python$,#!/usr/bin/python2,' {} \;
}

check() {
  cd ${_realname}-${pkgver}
  # we need utf locale to valid utf8 tests
  export LANG=en_US.UTF-8
  # Disable python3 check
  #msg2 'python checks'
  #PYTHONPATH="$PWD/build/python3/" ${MINGW_PREFIX}/bin/python3 test3/alltests.py
  msg2 'python2 checks'
  PYTHONPATH="$PWD/build/python2/" ${MINGW_PREFIX}/bin/python2 test/alltests.py
}

package_python3-docutils() {
  depends=("${MINGW_PACKAGE_PREFIX}-python3")

  local _mingw_prefix=$(cygpath -am ${MINGW_PREFIX})

  cd ${_realname}-${pkgver}
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/python3 setup.py build --build-lib=build/python3 \
      install --prefix=${MINGW_PREFIX} --root="${pkgdir}" --optimize=1

  # fix python-docutils conflict
  for _f in "${pkgdir}${MINGW_PREFIX}"/bin/*.py; do
    sed -e "s|${_mingw_prefix}/bin/|/usr/bin/env |g" -i ${_f}
    mv -v "$_f" "${_f%.py}3.py"
  done

  # symlink without .py
  for f in "${pkgdir}${MINGW_PREFIX}"/bin/*.py; do
    ln -s "$(basename $f)" "${pkgdir}${MINGW_PREFIX}/bin/$(basename $f .py)"
  done
  # setup license
  install -D -m644 COPYING.txt "${pkgdir}${MINGW_PREFIX}/share/licenses/python3-${_realname}/COPYING.txt"
  install -D -m644 licenses/python* "${pkgdir}${MINGW_PREFIX}/share/licenses/python3-${_realname}/"
}

package_python2-docutils() {
  depends=("${MINGW_PACKAGE_PREFIX}-python2")
  provides=("${MINGW_PACKAGE_PREFIX}-docutils=${pkgver}")
  replaces=("${MINGW_PACKAGE_PREFIX}-docutils")

  local _mingw_prefix=$(cygpath -am ${MINGW_PREFIX})

  cd ${_realname}-${pkgver}
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
  ${MINGW_PREFIX}/bin/python2 setup.py build --build-lib=build/python2 \
         install --prefix=${MINGW_PREFIX} --root="${pkgdir}" --optimize=1

  # fix python command in files
  for _f in "${pkgdir}${MINGW_PREFIX}"/bin/*.py; do
    sed -e "s|${_mingw_prefix}/bin/|/usr/bin/env |g" -i ${_f}
  done
  # symlink without .py
  for _f in "${pkgdir}${MINGW_PREFIX}"/bin/*.py; do
    ln -s "$(basename $_f)" "${pkgdir}${MINGW_PREFIX}/bin/$(basename $_f .py)"
  done
  # setup license
  install -D -m644 COPYING.txt "${pkgdir}${MINGW_PREFIX}/share/licenses/python2-${_realname}/COPYING.txt"
  install -D -m644 licenses/python* "${pkgdir}${MINGW_PREFIX}/share/licenses/python2-${_realname}/"
}

package_mingw-w64-i686-python2-docutils() {
  package_python2-docutils
}

package_mingw-w64-i686-python3-docutils() {
  package_python3-docutils
}

package_mingw-w64-x86_64-python2-docutils() {
  package_python2-docutils
}

package_mingw-w64-x86_64-python3-docutils() {
  package_python3-docutils
}
